package cn.oms.manage.controller;

import cn.oms.commons.pojo.vo.PageData;
import cn.oms.commons.web.JsonResult;
import cn.oms.manage.pojo.param.CustomerAddNewParam;
import cn.oms.manage.pojo.param.CustomerUpdateInfoParam;
import cn.oms.manage.pojo.vo.CustomerListItemVO;
import cn.oms.manage.pojo.vo.CustomerStandardVO;
import cn.oms.manage.service.ICustomerService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 说明：
 *
 * @author: Fcx
 * @date: 2023/6/16
 **/
@Slf4j
@RestController
@RequestMapping("/customers")
@Validated
@Api(tags = "01. 用户管理模块")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;

    public CustomerController() {
        log.debug("创建控制器类对象：CustomerController");
    }

    @PostMapping("/add-new")
    @PreAuthorize("hasAuthority('/customer/add-new')")
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid CustomerAddNewParam customerAddNewParam){
        log.debug("开始处理【添加用户】的请求，参数：{}", customerAddNewParam);
        customerService.addNew(customerAddNewParam);
        return JsonResult.ok();

    }

    @PostMapping("/{id:[0-9]+}/delete")
    @PreAuthorize("hasAuthority('/customer/delete')")
    @ApiOperation("根据ID删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult<Void> delete(@PathVariable @Range(min = 1, message = "请提交有效的用户ID值！") Long id) {
        log.debug("开始处理【根据ID删除用户】的请求，参数：{}", id);
        customerService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @PreAuthorize("hasAuthority('/customer/update')")
    @ApiOperation("修改用户详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult<Void> updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的用户ID值！") Long id,
                                           @Valid CustomerUpdateInfoParam customerUpdateInfoParam) {
        log.debug("开始处理【修改用户详情】的请求，ID：{}，新数据：{}", id, customerUpdateInfoParam);
        customerService.updateInfoById(id, customerUpdateInfoParam);
        return JsonResult.ok();
    }


    @GetMapping("/{id:[0-9]+}")
    @PreAuthorize("hasAuthority('/customer/read')")
    @ApiOperation("根据ID查询用户详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "long")
    })
    public JsonResult<CustomerStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的用户ID值！") Long id) {
        log.debug("开始处理【根据ID查询用户详情】的请求，参数：{}", id);
        CustomerStandardVO queryResult = customerService.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    @GetMapping("")
    @PreAuthorize("hasAuthority('/customer/read')")
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1", paramType = "query", dataType = "long"),
            @ApiImplicitParam(name = "queryType", value = "查询类型", example = "all")
    })
    public JsonResult<PageData<CustomerListItemVO>> list(@Range(min = 1, message = "请提交有效的页码值！") Integer page,
                                                         String queryType) {
        log.debug("开始处理【查询用户列表】的请求，页码：{}", page);
        Integer pageNum = page == null ? 1 : page;
        PageData<CustomerListItemVO> pageData;
        if ("all".equals(queryType)) {
            pageData = customerService.list(pageNum, Integer.MAX_VALUE);
        } else {
            pageData = customerService.list(pageNum);
        }
        return JsonResult.ok(pageData);
    }

}
